<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController
{
	public function index()
	{
		//print_r($_POST);
		//print_r($_SESSION);
		$question = D('vercode')->vercode();
		$this->assign('question',$question);

		$this->assign('title','登陆');
		$this->display();
	}

	//登陆
	public function login()
	{
		if (IS_POST) {
			$email = I("post.email");
			$pass = I("post.pass");
			$vercode = I("post.vercode");

			$userinfo = M('user')->where(array('email'=>$email))->find();
			if (!$userinfo) {
				$arr = array(
					"error" => 0,
					"info" => "邮箱不存在"
					);
				echo json_encode($arr);
				exit;
			}
			if ($userinfo['password']!=$pass) {
				$arr = array(
						"error" => 0,
						"info" => "密码输入有误"
					);
				echo json_encode($arr);
				exit;
			}

			if ($_SESSION['vercode_answer']) {
				if ($_SESSION['vercode_answer']!=$vercode) {
					$arr = array(
						"error" => 0,
						"info" => "人类问题错误"
						);
					echo json_encode($arr);
					exit;
				}
			}

			//更新登陆时间
			$login_time = time();
			M('user')->where(array('email'=>$email))->setField('login_time',$login_time);

			//设置session登录成功
			$_SESSION["id"] = $userinfo["id"];
			$_SESSION["username"] = $userinfo["username"];
			$_SESSION['face'] = $userinfo['face'];

			$arr = array(
					'error' =>1,
					'info'=>"登陆中..."
					);
			echo json_encode($arr);

			}
	}
	//退出登录
	public function logout()
	{
		session_unset();
		session_destroy();
		$this->redirect('Home/index/index');
	}


	//qq 回掉信息登陆   方法/地址
	public function qqcall()
	{
		$openid = qq_openid();
		//var_dump($openid);
		//检测是否登陆过
		$userinfo = M('user')->where(array('openid'=>$openid))->find();
		//print_r($userinfo);exit;
		if ($userinfo) {
			//登陆过
			//更新登陆时间
			$login_time = time();
			M('user')->where(array('openid'=>$openid))->setField('login_time',$login_time);

			//设置session登录成功
			$_SESSION["id"] = $userinfo["id"];
			$_SESSION["username"] = $userinfo["username"];
			$_SESSION['face'] = $userinfo['face'];
			$this->redirect('Home/user/myset');

		}else {
			//第一次登陆
			$qquserinfo = qq_userinfo();
			//var_dump($qquserinfo);
			$qquserinfo['create_time'] = time();
			$qquserinfo['create_ip'] = get_client_ip();
			$qquserinfo['username'] = $qquserinfo['nickname'].sj_Rand(4);
			$qquserinfo['sex'] = $qquserinfo['gender'];
			$qquserinfo['city'] = $qquserinfo['city'];

			$str = qq_face($qquserinfo['figureurl_2']);
			$str = substr($str, 1);
			$qquserinfo['face'] = $str;
			$qquserinfo['openid'] = $openid;

			$res = M('user')->add($qquserinfo);
			if ($res) {
				//设置session登录成功
				$_SESSION["id"] = $res;
				$_SESSION["username"] = $qquserinfo["username"];
				$_SESSION['face'] = $qquserinfo['face'];
				$this->redirect('Home/user/myset');
			}else {
				$this->redirect('Home/login/index');
			}

		}

	}

	//qq 登陆
	public function qqlogin()
	{
		qq_login();
		
	}



}

?>